<?php

namespace App\Repositories;

use App\Models\Ability;
use BobCoder\Construct\Foundation\Repository\Repository;
use Illuminate\Support\Facades\DB;

class AbilityRepository extends Repository
{
    protected $model;

    public function __construct()
    {
        $this->model = new Ability();
    }

    /**
     * 查询列表
     *
     * @param array $condition
     * @return mixed
     * @author  Bob<bob@bobcoder.cc>
     */
    public function getList($condition = [])
    {
        return $this->model->where($condition)->orderBy('sort', 'desc')->get();
    }

    /**
     * @param array $condition
     * @return mixed
     */
    public function firstWhere($condition = [])
    {
        return $this->model->where($condition)->first();
    }


    /**
     * @param array $params
     * @return Ability|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
     * @Author 姿势就是力量
     */
    public function saveData($params)
    {
        if (!empty($params['id'])) {
            $model = $this->find($params['id']);
        } else {
            $model = $this->model;
        }

        foreach ($params as $key => $value) {
            $model->{$key} = $value;
        }
        $model->save();

        return $model;
    }
}
